package com.heima.wemedia.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.file.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemeda.dto.WmMaterialDto;
import com.heima.model.wemeda.entity.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 16:16:26
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private WmMaterialDao wmMaterialDao;

    @Autowired
    private MinIOTemplate minIOTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }


    //分页查询
    @Override
    public ResponseResult queryByPage(WmMaterialDto wmMaterialDto) {
        if (wmMaterialDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //得到用户id
        Long userId = WmThreadLocalUtil.get();
        //分页查询
        Integer isCollection = wmMaterialDto.getIsCollection();
        wmMaterialDto.checkParam();//如果前端分页参数没有传递默认为1跟10
        PageHelper.startPage(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        Page<WmMaterial> page = wmMaterialDao.queryListByStatus(isCollection, userId);
        PageResponseResult result = new PageResponseResult(wmMaterialDto.getPage(), wmMaterialDto.getSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }

    @Override
    public ResponseResult updateBycancel(Long id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmMaterialDao.updateBycancel(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //收藏方法
    @Override
    public ResponseResult updateBycollect(Long id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmMaterialDao.updateBycollect(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }

    //上传图片
    @Override
    public ResponseResult<String> uploadPicture(MultipartFile multipartFile) {
        try {
            //1.判断文件是否存在
            if (multipartFile == null || multipartFile.getSize() <= 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "上传文件有误");
            }
            //上传文件名
            String originalFilename = multipartFile.getOriginalFilename();
            //文件名后缀
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            UUID uuid = UUID.randomUUID();
            originalFilename = uuid + substring;//最终文件名
            //将数据保存到minio中
            String url = minIOTemplate.uploadImgFile("", originalFilename, multipartFile.getInputStream());
            //将数据保存到数据表
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(WmThreadLocalUtil.get());
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setUrl(url);
            wmMaterial.setType(0);
            wmMaterial.setIsCollection(1);
            wmMaterial.setUpdatedTime(new Date());
            wmMaterial.setCreatedBy(null);
            wmMaterial.setUpdatedBy(null);
            wmMaterialDao.insert(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
